package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.cderp.domain.DicCity;
import com.ruoyi.cderp.mapper.DicCityMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.BaseDataAddressMapper;
import com.ruoyi.cderp.domain.BaseDataAddress;
import com.ruoyi.cderp.service.IBaseDataAddressService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 地址库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class BaseDataAddressServiceImpl implements IBaseDataAddressService
{
    @Autowired
    private BaseDataAddressMapper baseDataAddressMapper;

    @Autowired
    private DicCityMapper dicCityMapper;

    LoginUser loginUser = null;


    /**
     * 查询地址库
     *
     * @param id 地址库主键
     * @return 地址库
     */
    @Override
    public BaseDataAddress selectBaseDataAddressById(String id)
    {
        return baseDataAddressMapper.selectBaseDataAddressById(id);
    }

    /**
     * 查询地址库列表
     *
     * @param baseDataAddress 地址库
     * @return 地址库
     */
    @Override
    public List<BaseDataAddress> selectBaseDataAddressList(BaseDataAddress baseDataAddress)
    {
//        List<DicCity> cityIds = dicCityMapper.selectDicCityList();
        List<BaseDataAddress> list  = baseDataAddressMapper.selectBaseDataAddressList(baseDataAddress);
//        ArrayList list1 = new ArrayList();
//        for(int i=0;i<list.size();i++){
//                list1.add(list.get(i).getCityId().split("/"));
//        }
//        System.out.println(list1);
        return  list;
    }

    /**
     * 新增地址库
     *
     * @param baseDataAddress 地址库
     * @return 结果
     */
    @Override
    public int insertBaseDataAddress(BaseDataAddress baseDataAddress)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        baseDataAddress.setId("10" + formattedDate+randomNumber);
        baseDataAddress.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataAddress.setCreateById(loginUser.getUser().getUserId().toString());
        baseDataAddress.setCreateBy(loginUser.getUser().getUserName());
        baseDataAddress.setCreateTime(DateUtils.getNowDate());
        return baseDataAddressMapper.insertBaseDataAddress(baseDataAddress);
    }

    /**
     * 修改地址库
     *
     * @param baseDataAddress 地址库
     * @return 结果
     */
    @Override
    public int updateBaseDataAddress(BaseDataAddress baseDataAddress)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataAddress.setUpdateById(loginUser.getUser().getUserId().toString());
        baseDataAddress.setUpdateBy(loginUser.getUser().getUserName());
        baseDataAddress.setUpdateTime(DateUtils.getNowDate());
        return baseDataAddressMapper.updateBaseDataAddress(baseDataAddress);
    }

    /**
     * 批量删除地址库
     *
     * @param ids 需要删除的地址库主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataAddressByIds(String[] ids)
    {
        return baseDataAddressMapper.deleteBaseDataAddressByIds(ids);
    }

    /**
     * 删除地址库信息
     *
     * @param id 地址库主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataAddressById(String id)
    {
        return baseDataAddressMapper.deleteBaseDataAddressById(id);
    }
}
